package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.bill.MobileBillShowParameter;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.filter.FilterParameter;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.TabSelectEvent;
import kd.bos.form.control.events.TabSelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.list.events.ListRowClickEvent;
import kd.bos.list.events.ListRowClickListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 拼车司机端首页-行程单列表展示
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobDriver extends AbstractFormPlugin implements ListRowClickListener, TabSelectListener {

	private String[] buttons = new String[] { "generateorder", "orderlistdr", "gotodetail", "retodetail" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
		BillList billList = this.getControl("billlistap");
		billList.addListRowClickListener(this);
		Tab tab = this.getView().getControl("tabap");
		tab.addTabSelectListener(this);
	}

	@Override
	public void listRowClick(ListRowClickEvent evt) {
		ListRowClickListener.super.listRowClick(evt);
		IFormView view = this.getView();
		// 详情跳转
		BillList billList = this.getControl("billlistap");
		ListSelectedRow listSelectedRow = billList.getCurrentSelectedRowInfo();
		Long pk = (Long) listSelectedRow.getPrimaryKeyValue();
		MobileBillShowParameter showParameter = new MobileBillShowParameter();
		showParameter.setFormId("kdec_pincarbillmob");
		showParameter.setPkId(pk);
		showParameter.setCustomParam("pk", pk);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		view.showForm(showParameter);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		long userId = Long.valueOf(RequestContext.get().getUserId());
		// long orgId = RequestContext.get().getOrgId();
		Map<String, Object> params = new HashMap<>();
		if (StringUtils.equals("generateorder", key)) {// 接单
			BillList billList = this.getControl("billlistap");
			ListSelectedRow listSelectedRow = billList.getCurrentSelectedRowInfo();
			Long pk = (Long) listSelectedRow.getPrimaryKeyValue();
			DynamicObject obj = BusinessDataServiceHelper.loadSingleFromCache(pk, "kdec_pincarbill");
			QFilter detailFilter = new QFilter("creator", QCP.equals, userId);
			DynamicObject detail = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincardetailbill", "seatnum",
					detailFilter.toArray());
			int seatnum = detail.getInt("seatnum");
			QFilter filter = new QFilter("creator", QCP.equals, userId);
			filter.and(new QFilter("orderstatus", QCP.equals, '0'));
			filter.and(new QFilter("traveldate", QCP.equals, obj.get("traveldate")));
			DynamicObject[] orderObjs = BusinessDataServiceHelper.load("kdec_pincarorder", "memnum", filter.toArray());
			if (obj.getInt("memnum") > seatnum) {
				view.showMessage("接单人数大于座位数");
				return;
			} else if (obj.getInt("memnum") <= seatnum) {
				// 计算座位数
				if (null != orderObjs && orderObjs.length > 0) {
					int memnum = 0;
					for (DynamicObject dynamicObject : orderObjs) {
						memnum += dynamicObject.getInt("memnum");
					}
					if (memnum != 0) {
						if (memnum >= seatnum||(memnum+obj.getInt("memnum"))>seatnum) {
							Map<String, Object> formId = new HashMap<>();
							formId.put("formId", "kdec_pincartan");
							FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
							showParameter.getOpenStyle().setShowType(ShowType.Modal);
							view.showForm(showParameter);
							return;
						}
					}
				}
			}
			DynamicObject creator = (DynamicObject) obj.get("creator");
			params.put("formId", "kdec_pincarordermob");
			Map<String, Object> data = new HashMap<>();
			data.put("startplace", obj.get("startplace").toString());// 出发地
			data.put("endplace", obj.get("endplace"));// 目的地
			data.put("traveldate", obj.get("traveldate"));// 出发日期
			data.put("timefield", obj.get("timefield"));// 时间
			data.put("memnum", obj.get("memnum").toString());// 人数
			data.put("distsance", obj.get("distsance") + "km");// 距离
			data.put("taxicost", obj.get("taxicost"));// 推荐车费
			data.put("phone", creator.get("phone"));// 乘客手机号
			data.put("pk", pk);// 行程单主键ID
			this.showForm(params, data);
		} else if (StringUtils.equals("orderlistdr", key)) {// 订单列表页面跳转
			params.put("formId", "kdec_pincarordermoblist");
			this.showForm(params, null);
		} else if (StringUtils.equals("gotodetail", key) || StringUtils.equals("retodetail", key)) {// 前往注册
			params.put("formId", "kdec_pincardetailbillmob");
			this.showForm(params, null);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		long userId = Long.valueOf(RequestContext.get().getUserId());
		QFilter filterde = new QFilter("creator", QCP.equals, userId);
		IFormView view = this.getView();
		DynamicObject obj = BusinessDataServiceHelper.loadSingle("kdec_pincardetailbill", "id,billstatus,prob",
				filterde.toArray());
		if (obj == null) {// 无车主信息前往注册
			view.setVisible(true, "todetail");
			view.setVisible(false, "billlistap");
			view.setVisible(false, "mtoolbarap");
		} else if (null != obj && StringUtils.equals("B", obj.getString("billstatus"))) {// 注册过正在审核
			view.setVisible(true, "shing");
			view.setVisible(false, "billlistap");
			view.setVisible(false, "mtoolbarap");
		} else if (null != obj && StringUtils.equals("Z", obj.getString("billstatus"))) {// 注册过未通过审核
			view.setVisible(true, "redetail");
			view.setVisible(false, "billlistap");
			view.setVisible(false, "mtoolbarap");
			Label label = this.getControl("renews");
			label.setText(obj.getString("prob"));
		} else if (null != obj && StringUtils.equals("C", obj.getString("billstatus"))) {// 审核通过
			BillList billList = this.getControl("billlistap");
			int count = billList.getListModel().getBillDataCount();
			if (count == 0) {
				view.setVisible(true, "flexpanelap");
				view.setVisible(false, "billlistap");
			}
		}

	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		Tab tab = this.getView().getControl("tabap");
		tab.activeTab("tabdriver");
		BillList billList = this.getControl("billlistap");
		FilterParameter filterParameter = new FilterParameter();
		QFilter filter = new QFilter("orderstatus", QCP.equals, '0');
		filterParameter.setFilter(filter);
		billList.setFilterParameter(filterParameter);
	}

	@Override
	public void tabSelected(TabSelectEvent arg0) {
		String tabKey = arg0.getTabKey();
		if (StringUtils.equals(tabKey, "tabpageap")) {
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_pincar");
			this.showForm(formId, null);
		}
	}
	/**
	 * 页面跳转通用方法
	 * 
	 * @param formid
	 *            页面标识
	 * @param closeActionId
	 *            close回写标识
	 * @param customParams
	 *            传入下个页面参数
	 */
	private void showForm(Map<String, Object> params, Map<String, Object> customParams) {
		Map<String, Object> formId = new HashMap<>();
		formId.put("formId", params.get("formId"));
		FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		if (null != params.get("closeActionId")) {
			CloseCallBack closeCallBack = new CloseCallBack(this, params.get("closeActionId").toString());
			showParameter.setCloseCallBack(closeCallBack);
		}
		if (null != customParams) {
			showParameter.setCustomParams(customParams);
		}
		this.getView().showForm(showParameter);
	}
}
